﻿
@inject IHttpService _httpService
@inject IDataCacheService _dataCacheService
@inject NavigationManager NavigationManager
@inject IPageModelCatche<List<UpcomingGameItemModel>> _pageModelCatche

<section class="upcoming-games-card">
    <CnGalWebSite.Shared.MasaComponent.PC.Home.Cards.Shared.SlideGroup Title="即将发布" Items="_items.Take(_dataCacheService.IsApp?9:16)" MoreLink="/times">
        <MDCard Class="item" Href="@context.Url">
            <img src="@context.Image" alt="@context.Name" loading="lazy" />
            <div class="content">
                <div class="name text-truncate-1 title-large">@context.Name</div>
                <div class="brief text-truncate-3 label-large">@context.BriefIntroduction</div>
                <div class="tags">
                    <CnGalWebSite.Shared.MasaComponent.PC.Home.Cards.Shared.IconChip Color="@GetPublishTimeChipColor(context)" Icon="@GetPublishTimeChipIcon(context)" Text="@GetPublishTimeChipText(context)" />
                </div>
            </div>
        </MDCard>
    </CnGalWebSite.Shared.MasaComponent.PC.Home.Cards.Shared.SlideGroup>
</section>

@code {

    List<UpcomingGameItemModel> _items { get; set; } = new List<UpcomingGameItemModel>();

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }


    protected override async Task OnInitializedAsync()
    {
        try
        {
            //获取主页信息
            _items = await _pageModelCatche.GetCache("api/home/ListUpcomingGames");
            StateHasChanged();
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "获取主页即将发布游戏列表失败");
        }
    }

    string GetPublishTimeChipColor(UpcomingGameItemModel model)
    {
        return DateTime.TryParse(model.PublishTime, out DateTime time) ? "success" : "info";
    }

    IconType GetPublishTimeChipIcon(UpcomingGameItemModel model)
    {
        return DateTime.TryParse(model.PublishTime, out DateTime time) ? IconType.DeterminedTime : IconType.IndeterminateTime;
    }

    string GetPublishTimeChipText(UpcomingGameItemModel model)
    {
        return string.IsNullOrWhiteSpace(model.PublishTime) ? "未知" : model.PublishTime;
    }

}
